package com.common.entity;

import com.sqlsession.database.CTable;
import com.sqlsession.database.DbType;

import java.util.*;

public class CClass {

    public CClass(CTable cTable) {
        this.cTable = cTable;
    }

    public DbType getDbType() {
        return cTable.getDbType();
    }

    private CTable cTable;

    /**
     * 类中的属性列表
     */
    private List<CField> cFields;

    /**
     * 类中的方法列表
     */
    private List<CMethod> cMethods;

    /**
     * 类的包名
     */
    private String packages;

    /**
     * 类中需要导入的包
     */
    private Set<String> imports;

    /**
     * 类名称
     */
    private String name;

    /**
     * 类上面的注解
     */
    private Map<String, Map<String, String>> annotations = new HashMap<>();

    /**
     * 类注释
     */
    private String common;

    public String getCommon() {
        return common;
    }

    public void setCommon(String common) {
        this.common = common;
    }

    public Map<String, Map<String, String>> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(Map<String, Map<String, String>> annotations) {
        this.annotations = annotations;
    }

    public List<CField> getcFields() {
        return cFields;
    }

    public void setcFields(List<CField> cFields) {
        this.cFields = cFields;
    }

    public List<CMethod> getcMethods() {
        return cMethods;
    }

    public void setcMethods(List<CMethod> cMethods) {
        this.cMethods = cMethods;
    }

    public Set<String> getImports() {
        if (imports == null) {
            imports = new HashSet<>();
        }
        for (CField cField : cFields) {
            Map<String, String> classType = cField.getClassType();
            for (Map.Entry<String, String> entry : classType.entrySet()) {
                imports.add(entry.getValue());
            }
            Map<String, Map<Set<String>, String>> cFieldAnnotations = cField.getAnnotations();
            for (Map.Entry<String, Map<Set<String>, String>> entry : cFieldAnnotations.entrySet()) {
                Map<Set<String>, String> value = entry.getValue();
                for (Map.Entry<Set<String>, String> setStringEntry : value.entrySet()) {
                    Set<String> key = setStringEntry.getKey();
                    Iterator<String> iterator = key.iterator();
                    while (iterator.hasNext()) {
                        String next = iterator.next();
                        imports.add(next);
                    }
                }
            }
        }
        for (CMethod cMethod : cMethods) {
            List<Map<String, Map<String, String>>> param = cMethod.getParam();
            if (param != null) {
                for (Map<String, Map<String, String>> map : param) {
                    for (Map.Entry<String, Map<String, String>> entry : map.entrySet()) {
                        for (Map.Entry<String, String> stringStringEntry : entry.getValue().entrySet()) {
                            String key = stringStringEntry.getValue();
                            imports.add(key);
                        }
                    }
                }
            }
            Map<String, String> returnType = cMethod.getReturnType();
            if (returnType != null) {
                for (Map.Entry<String, String> entry : returnType.entrySet()) {
                    imports.add(entry.getValue());
                }
            }
        }
        Map<String, Map<String, String>> annotations = getAnnotations();
        for (Map.Entry<String, Map<String, String>> entry : annotations.entrySet()) {
            for (Map.Entry<String, String> stringEntry : entry.getValue().entrySet()) {
                imports.add(stringEntry.getKey());
            }
        }
        return imports;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CTable getcTable() {
        return cTable;
    }

    public void setcTable(CTable cTable) {
        this.cTable = cTable;
    }
}
